package com.mtw.bbs.util;



import org.springframework.data.elasticsearch.core.query.HighlightQuery;
import org.springframework.data.elasticsearch.core.query.highlight.Highlight;
import org.springframework.data.elasticsearch.core.query.highlight.HighlightField;
import org.springframework.data.elasticsearch.core.query.highlight.HighlightFieldParameters;

import java.util.ArrayList;
import java.util.List;

/**
 * elasticsearch工具类
 */
public class ESUtils {


    public static HighlightQuery getHighlightQuery(Class<?>  esIndex,String... fields){

        //高亮查询
        List<HighlightField> highlightFieldList = new ArrayList<>();
        for (String field : fields) {
            HighlightField highlightField = new HighlightField(field, HighlightFieldParameters.builder().withPreTags("<em>").withPostTags("</em>").build());
            highlightFieldList.add(highlightField);
        }
        Highlight highlight = new Highlight(highlightFieldList);
        return new HighlightQuery(highlight, esIndex);
    }
}
